using System;
using System.Data.SQLite;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

namespace VocabularyApp
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new VocabularyForm());
        }
    }

    public class VocabularyForm : Form
    {
        private Label lblChinese;
        private TextBox txtEnglish;
        private Label lblResult;
        private Button btnNext;
        private SQLiteConnection connection;
        private DataTable wordsTable;
        private int currentIndex = 0;

        public VocabularyForm()
        {
            InitializeDatabase();
            InitializeComponents();
            LoadWords();
            ShowCurrentWord();
        }

        private void InitializeDatabase()
        {
            // 创建或连接SQLite数据库
            connection = new SQLiteConnection("Data Source=vocabulary.db;Version=3;");
            connection.Open();

            // 创建单词表（如果不存在）
            string createTableSql = @"
                CREATE TABLE IF NOT EXISTS Words (
                    Id INTEGER PRIMARY KEY AUTOINCREMENT,
                    English TEXT NOT NULL,
                    Chinese TEXT NOT NULL
                )";
            new SQLiteCommand(createTableSql, connection).ExecuteNonQuery();

            if (Convert.ToInt32(new SQLiteCommand("SELECT COUNT(*) FROM Words", connection).ExecuteScalar()) == 0)
            {
                string[] words = {
                    "INSERT INTO Words (English, Chinese) VALUES ('persistent', '坚持不懈的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('diligent', '勤奋的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('ambitious', '有雄心的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('resilient', '适应力强的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('versatile', '多才多艺的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('meticulous', '一丝不苟的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('profound', '深刻的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('eloquent', '雄辩的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('innovative', '创新的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('tenacious', '顽强的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('altruistic', '利他的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('sagacious', '睿智的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('ephemeral', '短暂的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('ubiquitous', '无处不在的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('fastidious', '挑剔的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('gregarious', '爱社交的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('quintessential', '典型的')",
                    "INSERT INTO Words (English, Chinese) VALUES ('serendipity', '意外发现珍奇事物的本领')",
                    "INSERT INTO Words (English, Chinese) VALUES ('equanimity', '镇定')",
                    "INSERT INTO Words (English, Chinese) VALUES ('magnanimous', '宽宏大量的')"
                };

                foreach (var sql in words)
                {
                    new SQLiteCommand(sql, connection).ExecuteNonQuery();
                }
            }
        }

        private void LoadWords()
        {
            var adapter = new SQLiteDataAdapter("SELECT * FROM Words", connection);
            wordsTable = new DataTable();
            adapter.Fill(wordsTable);
        }

        private void InitializeComponents()
        {
            this.Text = "背单词程序";
            this.Size = new Size(400, 300);
            this.StartPosition = FormStartPosition.CenterScreen;

            lblChinese = new Label
            {
                Text = "中文词义将显示在这里",
                Font = new Font("Microsoft YaHei", 14),
                AutoSize = false,
                TextAlign = ContentAlignment.MiddleCenter,
                Size = new Size(350, 50),
                Location = new Point(25, 50)
            };

            // 英文输入框
            txtEnglish = new TextBox
            {
                Font = new Font("Microsoft YaHei", 12),
                Size = new Size(350, 30),
                Location = new Point(25, 120)
            };
            txtEnglish.KeyPress += TxtEnglish_KeyPress;

            // 结果显示标签
            lblResult = new Label
            {
                Text = "",
                Font = new Font("Microsoft YaHei", 12),
                AutoSize = false,
                TextAlign = ContentAlignment.MiddleCenter,
                Size = new Size(350, 30),
                Location = new Point(25, 160),
                ForeColor = Color.Blue
            };

            btnNext = new Button
            {
                Text = "下一个",
                Font = new Font("Microsoft YaHei", 12),
                Size = new Size(100, 40),
                Location = new Point(150, 200)
            };
            btnNext.Click += BtnNext_Click;

            this.Controls.Add(lblChinese);
            this.Controls.Add(txtEnglish);
            this.Controls.Add(lblResult);
            this.Controls.Add(btnNext);
        }

        private void ShowCurrentWord()
        {
            if (wordsTable.Rows.Count == 0)
            {
                lblChinese.Text = "没有单词可显示";
                txtEnglish.Enabled = false;
                return;
            }

            // 显示当前单词的中文意思
            lblChinese.Text = wordsTable.Rows[currentIndex]["Chinese"].ToString();
            txtEnglish.Text = "";
            lblResult.Text = "";
            txtEnglish.Focus();
        }

        private void TxtEnglish_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                CheckAnswer();
                e.Handled = true;
            }
        }

        private void CheckAnswer()
        {
            if (wordsTable.Rows.Count == 0) return;

            string userAnswer = txtEnglish.Text.Trim();
            string correctAnswer = wordsTable.Rows[currentIndex]["English"].ToString();

            if (string.Equals(userAnswer, correctAnswer, StringComparison.OrdinalIgnoreCase))
            {
                lblResult.Text = "正确!";
            }
            else
            {
                lblResult.Text = $"错误! 正确答案是: {correctAnswer}";
            }
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            if (wordsTable.Rows.Count == 0) return;

            currentIndex = (currentIndex + 1) % wordsTable.Rows.Count;
            ShowCurrentWord();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                connection?.Close();
                connection?.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}